// 验证码倒计时
var time = 60;
var state = false;

resetTime();

$(function () {
    validateRule();
    $(".i-checks").iCheck({checkboxClass: "icheckbox_square-green-login"});
    $('.imgcode').click(function () {
        var url = ctx + "captcha/captchaImage?type=" + captchaType + "&s=" + Math.random();
        $(".imgcode").attr("src", url);
    });
});

$.validator.setDefaults({
    submitHandler: function () {
        login();
    }
});

function login() {
    $.modal.loading($("#btnSubmit").data("loading"));
    var username = $("input[name='username']").val().trim();
    var password = $("input[name='password']").val().trim();
    var validateCode = $("input[name='validateCode']").val();
    var smsValidateCode = $("input[name='smsValidateCode']").val();
    var rememberMe = $("input[name='rememberme']").is(':checked');
    $.ajax({
        type: "post",
        url: ctx + "login",
        data: {
            "username": username,
            "password": password,
            "validateCode": validateCode,
            "smsValidateCode": smsValidateCode,
            "rememberMe": rememberMe
        },
        success: function (r) {
            if (r.code == 0) {
                location.href = ctx + 'index';
            } else {
                $.modal.closeLoading();
                $('.imgcode').click();
                $.modal.msg(r.msg);
            }
        }
    });
}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            username: {
                required: true
            },
            password: {
                required: true
            }
        },
        messages: {
            username: {
                required: icon + "请输入您的用户名",
            },
            password: {
                required: icon + "请输入您的密码",
            }
        }
    })
}

function resetTime() {
    var s = $.layout.state.getCookieValue("secondsremained");
    var timer;
    if (s > 0) {
        var btn = document.getElementById("getSmsCode");
        btn.setAttribute("class", "sms_validateCode remesscode");
        state = true;

        function countDown() {
            s--;
            $("#getSmsCode").html(s + "秒");
            if (s <= 0) {
                $("#getSmsCode").html("短信验证");
                clearInterval(timer);
                state = false;
                btn.setAttribute("class", "sms_validateCode");
            }
            $.layout.state.editCookie("secondsremained", s, s + 1);
        }

        timer = setInterval(countDown, 1000);
    }
}

function sendSms() {
    if (state) {
        return;
    }
    var username = $("input[name='username']").val().trim();
    var password = $("input[name='password']").val().trim();
    if (username === '' || password === '') {
        $.modal.msg("请输入用户名密码");
        return;
    }
    var config = {
        url: "/sms",
        type: "post",
        dataType: "json",
        data: {
            "username": username,
            "password": password,
        },
        success: function (result) {
            if (result.code === 0) {
                $.layout.state.addCookie("secondsremained", 60, 60);
                resetTime();
            } else {
                alert(result.msg);
            }
        }
    };
    $.ajax(config);
}


